package com.baibei.order.list;

import android.support.v7.util.DiffUtil;

import com.baibei.model.OrderInfo;

import java.util.List;

/**
 * 数据比较，局部刷新
 * 避免数据刷新，提高显示效率以及用户体验
 * Created by shenminjie on 2017/8/14.
 * mail:shenminjie@100bei.com
 */

public class DiffCallBack extends DiffUtil.Callback {

    /**
     * 新旧数据源
     */
    private List<OrderInfo> mOldInfos;
    private List<OrderInfo> mNewsInfos;

    public DiffCallBack(List<OrderInfo> olds, List<OrderInfo> news) {
        this.mOldInfos = olds;
        this.mNewsInfos = news;
    }

    @Override
    public int getOldListSize() {
        return mOldInfos == null ? 0 : mOldInfos.size();
    }

    @Override
    public int getNewListSize() {
        return mNewsInfos == null ? 0 : mNewsInfos.size();
    }

    @Override
    public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
        OrderInfo oldItem = mOldInfos.get(oldItemPosition);
        OrderInfo newItem = mNewsInfos.get(newItemPosition);
        return oldItem.getOrderid().equals(newItem.getOrderid());
    }

    @Override
    public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
        OrderInfo oldItem = mOldInfos.get(oldItemPosition);
        OrderInfo newItem = mNewsInfos.get(newItemPosition);
        if (!oldItem.getArea().equals(newItem.getArea())) {
            return false;
        }
        if (!oldItem.getProductpic().equals(newItem.getProductpic())) {
            return false;
        }
        if (!oldItem.getName().equals(newItem.getName())) {
            return false;
        }
        if (oldItem.getBuydirection() != (newItem.getBuydirection())) {
            return false;
        }
        if (oldItem.getBuyprice() != newItem.getBuyprice()) {
            return false;
        }
        if (oldItem.getLast() != newItem.getLast()) {
            return false;
        }
        if (oldItem.getCount() != newItem.getCount()) {
            return false;
        }
        //根据最新价格局部刷新
        return true;
    }
}
